import { execute, select } from '../../utils/db';
import { TrainRecord } from '../../types/trainRecord';
import { Trainer } from '@/types/trainer';

export class TrainRecordDao {
  // 新增训练记录
  static async add(record: Omit<TrainRecord, 'id'>): Promise<number> {
    const sql = `INSERT INTO train_record (course_id, slot_id, trainer_id, scense_id) 
                 VALUES (?, ?, ?, ?)`;
    const result = await execute(sql, [
      record.course_id, 
      record.slot_id,
      record.trainer_id,
      record.scense_id
    ]);
    return result.lastInsertId;
  }

  // 更新训练记录
  static async update(record: TrainRecord): Promise<void> {
    const sql = `UPDATE train_record SET 
                 course_id = ?, slot_id = ?, trainer_id = ?, scense_id = ?
                 WHERE id = ?`;
    await execute(sql, [
      record.course_id,
      record.slot_id,
      record.trainer_id,
      record.scense_id,
      record.id
    ]);
  }

    // 更新训练记录
  static async updateBySlotIdAndTrainerId(record: TrainRecord): Promise<void> {
    const sql = `UPDATE train_record SET scense_id = ? WHERE slot_id = ? and trainer_id = ?`;
    await execute(sql, [
      record.scense_id,
      record.slot_id,
      record.trainer_id
    ]);
  }

  // 删除训练记录
  static async delete(id: number): Promise<void> {
    const sql = 'DELETE FROM train_record WHERE id = ?';
    await execute(sql, [id]);
  }

    // 删除训练记录
  static async deleteBySoltId(solt_id: number): Promise<void> {
    const sql = 'DELETE FROM train_record WHERE slot_id = ?';
    await execute(sql, [solt_id]);
  }

  // 根据ID查询
  static async getById(id: number): Promise<TrainRecord | null> {
    const sql = 'SELECT * FROM train_record WHERE id = ?';
    const result = await select<TrainRecord[]>(sql, [id]);
    return result[0] || null;
  }

  // 查询所有记录
  static async getAll(): Promise<TrainRecord[]> {
    const sql = 'SELECT * FROM train_record';
    return await select<TrainRecord[]>(sql);
  }

  // 通用查询方法
  static async query(sql: string, params?: any[]): Promise<TrainRecord[]> {
    return await select<TrainRecord[]>(sql, params);
  }

   static async list( trainRecord: TrainRecord): Promise<TrainRecord[]> {
  
      const fields = []
      const values = []
  
      if (trainRecord.course_id) {
        fields.push('course_id = ?')
        values.push(trainRecord.course_id)
      }
      if (trainRecord.slot_id) {
        fields.push('slot_id = ?')
        values.push(trainRecord.slot_id)
      }
      if (trainRecord.trainer_id) {
        fields.push('trainer_id = ?')
        values.push(trainRecord.trainer_id)
      }
      if (trainRecord.scense_id) {
        fields.push('scense_id = ?')
        values.push(trainRecord.scense_id)
      }
      const condition = fields.length > 0 ? `WHERE ${fields.join(' AND ')}` : ''
      const result = await select<TrainRecord[]>(
        `SELECT * FROM train_record ${condition}`,
        values
      )
      return result
    }

  // 获取记录数
  static async getCount(sql: string, params?: any[]): Promise<{total: number}> {
    const result = await select<{total: number}[]>(sql, params);
    return result[0];
  }

}